<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\heepay\model;

class Member extends \app\BaseModel
{
    protected $name = "massage_heepay_user_list";
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function adminList($dis, $page, $where = [])
    {
        $data = $this->alias("a")->join("massage_service_user_list c", "a.user_id = c.id", "left")->where($dis)->where(function ($query) {
            $query->whereOr($where);
        })->field("a.*,c.nickName")->group("a.id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function updateOne($id)
    {
        $user_id = $this->where(["id" => $id])->value("user_id");
        $res = $this->where(["user_id" => $user_id])->where("id", "<>", $id)->update(["status" => 0]);
        return $res;
    }
    public function checkUserCash($user_id, $tx_cash)
    {
        $dis = ["user_id" => $user_id, "status" => 1];
        $member = $this->dataInfo($dis);
        if(empty($member)) {
            return ["code" => 500, "msg" => "该用户没有账户"];
        }
        $heepay = new \longbingcore\heepay\HeePay($member["uniacid"]);
        $user_info = $heepay->userResInfo($member["apply_no"]);
        if(empty($user_info)) {
            return ["code" => 500, "msg" => "用户信息错误"];
        }
        if(empty($user_info["biz_content"]["audit_status"]) || $user_info["biz_content"]["audit_status"] != 3) {
            return ["code" => 500, "msg" => "用户信息未审核通过"];
        }
        $cash = $heepay->agentCash($user_info["biz_content"]["heepay_id"]);
        if(!isset($cash["can_Used_Amt"])) {
            return ["code" => 500, "msg" => "用户信息错误"];
        }
        $can_Used_Amt = $cash["can_Used_Amt"] - $cash["lock_Amt"];
        if($can_Used_Amt < $tx_cash) {
            $now_cash = $tx_cash - $can_Used_Amt;
            $order_code = orderCode();
            $res = $heepay->agentRechargev2($member["contact_email"], $order_code, $now_cash, $member["bank_account_name"]);
            if(!isset($res["ret_code"]) || $res["ret_code"] != "0000") {
                return ["code" => 500, "msg" => $res["ret_msg"]];
            }
            $insert = ["uniacid" => $member["uniacid"], "apply_no" => $member["apply_no"], "heepay_id" => $user_info["biz_content"]["heepay_id"], "cash" => $now_cash, "agent_bill_id" => $order_code];
            $record_model = new RechargeRecord();
            $record_model->dataAdd($insert);
        }
        return ["heepay_id" => $user_info["biz_content"]["heepay_id"]];
    }
}

?>